package com.ecoolex.ying.space.core.manager;

import com.ecoolex.ying.space.core.dao.EstateProjectActivityGoodsOrderDetailDAO;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoods;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrder;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderDetail;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderDetailExample;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderDetailExample.Criteria;
import com.ecoolex.ying.space.pojo.SubmitEstateProjectActivityGoodsOrderDetailDTO;

import java.math.BigDecimal;
import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EstateProjectActivityGoodsOrderDetailManager {
	
    @Autowired
    private EstateProjectActivityGoodsOrderDetailDAO dao;
    
    @Autowired
    private EstateProjectActivityGoodsManager goodsManager;
    
    public void add(EstateProjectActivityGoodsOrder order,List<SubmitEstateProjectActivityGoodsOrderDetailDTO> detailList,String createUser) {
    	for (SubmitEstateProjectActivityGoodsOrderDetailDTO detailDTO : detailList) {
    		EstateProjectActivityGoods estateProjectActivityGoods = goodsManager.getEstateProjectActivityGoods(detailDTO.getActivityGoodsId());
			EstateProjectActivityGoodsOrderDetail detail = new EstateProjectActivityGoodsOrderDetail();
			detail.setActivityGoodsId(detailDTO.getActivityGoodsId());
			detail.setActivityGoodsName(estateProjectActivityGoods.getActivityName());
			detail.setBuyNumber(detailDTO.getBuyNumber());
			detail.setCoverImage(estateProjectActivityGoods.getActivityCoverImage());
			detail.setOrderId(order.getOrderId());
			detail.setRetailPrice(estateProjectActivityGoods.getActivityPrice());
			detail.setTotalPrice(estateProjectActivityGoods.getActivityPrice().multiply(BigDecimal.valueOf(detailDTO.getBuyNumber())));
			detail.setDeleted(EstateProjectActivityGoodsOrderDetail.NOT_DELETED);
			detail.setCreateUser(createUser);
			detail.setCreateTime(Date.from(Instant.now()));
			detail.setUpdateUser(createUser);
			detail.setUpdateTime(Date.from(Instant.now()));
	    	dao.insert(detail);
		}
    }
    
    public List<EstateProjectActivityGoodsOrderDetail> detailList(Integer orderId){
    	EstateProjectActivityGoodsOrderDetailExample example = new EstateProjectActivityGoodsOrderDetailExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andOrderIdEqualTo(orderId).andDeletedEqualTo(EstateProjectActivityGoodsOrderDetail.NOT_DELETED);
    	return dao.selectByExample(example);
    }
}